package com.asdan.seededu.util;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * 获取Session对象
 * @author user
 *
 */
public class Util {
	
	public static int pageCount = 2;     // 分页显示 每页的条数
	// 静态代码块   实例SessionFactory对象
//	static{
//		System.out.println(new Configuration().configure());
//		sf = new Configuration().configure().buildSessionFactory();
//		System.out.println(sf);
//	}
	/**
	 * 返回Session对象
	 * @return
	 */

	
	
	
	public static String getTimeOut(String startTime,String endTime){
		//按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		long nd = 1000*24*60*60;//一天的毫秒数
		long nh = 1000*60*60;//一小时的毫秒数
		long nm = 1000*60;//一分钟的毫秒数
		long ns = 1000;//一秒钟的毫秒数long diff;try {
		//获得两个时间的毫秒时间差异
		try{
		long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
		long day = diff/nd;//计算差多少天
		System.out.println("时间相差："+day+"天");
		return day + "天";
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("");
		}
		return "";
//		long hour = diff%nd/nh;//计算差多少小时
//		long min = diff%nd%nh/nm;//计算差多少分钟
//		long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
//		System.out.println("时间相差："+day+"天");//+hour+"小时"+min+"分钟"+sec+"秒。");
	}
	
	public static String getLastDaterange(){
		String date = "";
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
		Calendar c=Calendar.getInstance(Locale.CHINA);
		//当前时间，貌似多余，其实是为了所有可能的系统一致
		c.setTimeInMillis(System.currentTimeMillis());
		System.out.println("当前时间:"+sf.format(c.getTime()));
		c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		date = sf.format(c.getTime());
		System.out.println("周日时间:"+sf.format(c.getTime()));
		c.add(Calendar.DAY_OF_YEAR, -6);
		date = sf.format(c.getTime()) + "至" + date;
		System.out.println(sf.format(c.getTime()));
		return date;
	}
	
	public static String getDaterange(){
		String date = "";
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
		Calendar c=Calendar.getInstance(Locale.CHINA);
		//当前时间，貌似多余，其实是为了所有可能的系统一致
		c.setTimeInMillis(System.currentTimeMillis());
		System.out.println("当前时间:"+sf.format(c.getTime()));
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		date = sf.format(c.getTime());
		System.out.println("周一时间:"+sf.format(c.getTime()));
		c.add(Calendar.DAY_OF_YEAR, 6);
		date += "至"+sf.format(c.getTime());
		System.out.println(sf.format(c.getTime()));
		return date;
	}
	
	public static String getDate(){
		String date = "";
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
		Calendar c=Calendar.getInstance(Locale.CHINA);
		//当前时间，貌似多余，其实是为了所有可能的系统一致
		c.setTimeInMillis(System.currentTimeMillis());
		System.out.println("当前时间:"+sf.format(c.getTime()));
//		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		date = sf.format(c.getTime());
//		System.out.println("周一时间:"+sf.format(c.getTime()));
//		c.add(Calendar.DAY_OF_YEAR, 6);
//		date += "至"+sf.format(c.getTime());
//		System.out.println(sf.format(c.getTime()));
		return date;
	}
	
	public static String getDateShow(){
		String date = "";
		SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日",Locale.CHINA);
		Calendar c=Calendar.getInstance(Locale.CHINA);
		//当前时间，貌似多余，其实是为了所有可能的系统一致
		c.setTimeInMillis(System.currentTimeMillis());
		System.out.println("当前时间:"+sf.format(c.getTime()));
		date = sf.format(c.getTime());
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        
        int w = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return date + " " + weekDays[w];

		
//		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		
//		System.out.println("周一时间:"+sf.format(c.getTime()));
//		c.add(Calendar.DAY_OF_YEAR, 6);
//		date += "至"+sf.format(c.getTime());
//		System.out.println(sf.format(c.getTime()));
//		return date;
	}
	
	public static String getString(String str){
		if(str==null){
			return "无";
		}else{
			return str;
		}
		
	}
	
	public static String getStr(String str){
		if(str==null){
			return "";
		}else{
			return str;
		}
		
	}
	
	public static String getTdStr(String str){
		if(str==null){
			return "&nbsp;";
		}else{
			return str;
		}
	}
}
